/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_wtc_report_msg_data_req.c                       */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include "../../../src/module/tuxedowtc/config.h"
#include "../test_wtc_comm.h"
#include <tuxedowtc/wtc_core.h>
#include <tuxedowtc/wtc_header.h>
#include <tuxedowtc/wtc_comm.h>
#include <tuxedowtc/wtc_packet.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body = NULL;

    CU_ASSERT_EQUAL(test_wtc_create_ct(&p_st_conn, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_wtc_private *p_st_session_private = (L7session_wtc_private *) frct_get_session_private(p_st_session);
    L7session_wtc *p_st_sess = p_st_session_private->p_st_sess;
    L7_wtc_packet *p_st_wtc_packet = p_st_session_private->p_st_wtc_packet;

    char *p_bin = (char *) "000000000000001400000000fffffffe000001b0800000010000000000000000910398580000001100010000000000000000002c0000123000000006535452494e47000000000000000000096c6f77657263617365000000001300000000005400001230ffffd8f0000000487fffffff00000003000000000000001003c9cee40000bea10bc9ced50000000900000001000000000dc9cee5000000120000000a00006c5e1fa3e5e6703a00000007000000000058000012300000000100000002ffffffff0000000000000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000007544f555050455200000a0000000000ac000012300000001e00000020000000000000000000000000000000000000006c0000005e0000001fffffffa3ffffffe5ffffffe6000000700000003a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a5754434c6f63616c41500000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(wtc_packet_unfragment(p_st_sess, p_st_packet, p_st_wtc_packet), APM_WTC_CHECK_FRAGMENT_SUCCESS);

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    p_st_body = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, "test"), FR_SUCC);

    CU_ASSERT_EQUAL(wtc_report_msg_data_req(p_st_conn, p_st_sess, p_st_wtc_packet, p_st_body, &st_hash_report), FR_SUCC);

    Frvar_field *p_st_field = NULL;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);

    p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->p_field_name, "transaction.title.s");
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "test");

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_wtc_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_wtc_report_msg_data_req(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" wtc_report_msg_data_req()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
